package com.lixin.juc.callable;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * Callable的使用
 */
public class MainApplication {
    public static void main(String[] args) {
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            System.out.println("任务开始执行");
            TimeUnit.SECONDS.sleep(3);
            return "ok";
        });
        //FutureTask<String> futureTask = new FutureTask<>(new MyCallable());
        new Thread(futureTask, "task").start();
        try {
            String result = futureTask.get(); // 同步获取线程执行结果，会产生阻塞
            System.out.println("任务执行结果：" + result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}
